package com.cms.service.impl;

import com.cms.dao.FreelanceMapper;
import com.cms.entity.Freelance;
import com.cms.service.FreelanceService;
import com.cms.exception.DuplicateKeyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class FreelanceServiceImpl implements FreelanceService {

    @Autowired
    private FreelanceMapper freelanceMapper;

    @Override
    public List<Freelance> findAll() {
        return freelanceMapper.findAll();
    }

    @Override
    public int insertFreelance(Freelance freelance) throws DuplicateKeyException {
        int result = 0;
        try {
            result = freelanceMapper.insertFreelance(freelance);
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry")) {
                //已存在相同编号
                throw new DuplicateKeyException();
            }else{
                throw e;
            }
        }
        return result;
    }

    @Override
    public int updateFreelance(HashMap<String,String> updateMap) throws DuplicateKeyException {
        int result = 0;
        try {
            result = freelanceMapper.updateFreelance(updateMap);
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry")) {
                //已存在相同编号
                throw new DuplicateKeyException();
            }else{
                throw e;
            }
        }
        return result;
    }

    @Override
    public int deleteFreelance(String id) {
        return freelanceMapper.deleteFreelance(id);
    }

    @Override
    public List<Freelance> findFreelanceById(String id) {
        return freelanceMapper.findFreelanceById(id);
    }

    @Override
    public List<Freelance> findFreelanceByName(String name) {
        return freelanceMapper.findFreelanceByName(name);
    }

    @Override
    public List<Freelance> findFreelanceByTelephone(String telephone) {
        return freelanceMapper.findFreelanceByTelephone(telephone);
    }

    @Override
    public List<Freelance> findFreelanceByEmail(String email) {
        return freelanceMapper.findFreelanceByEmail(email);
    }

    @Override
    public List<Freelance> findFreelanceByType(String type) {
        return freelanceMapper.findFreelanceByType(type);
    }


}
